package com.adjy.pathbuilders.screens;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.commons.FontLoader;
import com.adjy.pathbuilders.commons.FontLoader.Fonts;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.ImageButton;
import com.adjy.pathbuilders.objects.button.TextImageButton;
import com.adjy.pathbuilders.objects.button.listeners.ButtonListener;
import com.adjy.pathbuilders.objects.button.listeners.ChangeGameStateButtonListener;
import com.adjy.pathbuilders.objects.button.listeners.QuitGameButtonListener;
import com.adjy.pathbuilders.rendering.DisplayConverter;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.state.Game;
import com.adjy.pathbuilders.state.GameState;
import com.adjy.pathbuilders.world.GameWorld;
import com.adjy.pathbuilders.world.Level;
import com.adjy.pathbuilders.world.StageProgress;

public class PauseScreen implements GameScreen {
	
	private static final String PAUSED_BACKGROUND = "paused_background";
	
	private Paint textPaint;
	private Paint strokePaint;
	
	private AnimationFactory anFactory;
	private List<Button> buttons;
	private int bestTime;
	private int mostCoins;
	private GameWorld world;
	private Game game;
	
	public PauseScreen(AnimationFactory anFactory, Level level, GameWorld world, Game game) {
		this.anFactory = anFactory;
		this.bestTime = level.getBestTime();
		this.mostCoins = level.getMostCoins();
		
		buttons = new ArrayList<Button>();

		this.game = game;
		this.world = world;
		
		float textSize = FontLoader.getInstance().getFontSize() - 10;
		
		textPaint = new Paint();
		textPaint.setARGB(255, 255, 98, 61);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(textSize);
		textPaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));

		strokePaint = new Paint();
		strokePaint.setARGB(255, 172, 51, 22);
		strokePaint.setAntiAlias(true);
		strokePaint.setStrokeWidth(3);
		strokePaint.setTextSize(textSize);
		strokePaint.setStyle(Style.STROKE);
		strokePaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
		
	}
	
	private void createButtons() {
		if (buttons.size() == 0) {
			Button b = new TextImageButton(200, 210,
					new ChangeGameStateButtonListener(game, GameState.RESUME_GAME),
					new WeakReferenceBitmap(anFactory, "menu_button_selected"),
							new WeakReferenceBitmap(anFactory, "menu_button_unselected"),
							game.getContext().getString(R.string.resume));
			
			buttons.add(b);
			
			ButtonListener listener = new ButtonListener() {
				
				@Override
				public void onButtonPressed() {
					world.restart();
				}
			};
			
			b = new ImageButton(700, 20,
					listener,
					new WeakReferenceBitmap(anFactory, "restart_selected"),
							new WeakReferenceBitmap(anFactory, "restart_unselected"));
			
			buttons.add(b);
			
			listener = new ButtonListener() {
				
				@Override
				public void onButtonPressed() {
					world.getMainChar().setCurrentStageProgress(StageProgress.FINISHED);
					game.changeState(GameState.RESUME_GAME);
				}
			};
			
			b = new ImageButton(20, 20,
					listener,
					new WeakReferenceBitmap(anFactory, "next_selected"),
							new WeakReferenceBitmap(anFactory, "next_unselected"));
			
			buttons.add(b);
			
			b = new TextImageButton(200, 300,
					new ChangeGameStateButtonListener(game, GameState.MAIN_MENU),
					new WeakReferenceBitmap(anFactory, "menu_button_selected"),
							new WeakReferenceBitmap(anFactory, "menu_button_unselected"),
							game.getContext().getString(R.string.main_menu), textPaint, strokePaint);
			
			buttons.add(b);
			
			b = new TextImageButton(200, 390,
					new QuitGameButtonListener(game), 
					new WeakReferenceBitmap(anFactory, "menu_button_selected"),
							new WeakReferenceBitmap(anFactory, "menu_button_unselected"),
							game.getContext().getString(R.string.quit));
			
			buttons.add(b);
		}
	}
	
	public void step(float timePassed) {

	}

	@Override
	public void render(GameCanvas canvas, float timeElapsed) {
		world.render(canvas, 0);
		canvas.drawARGB(200, 255, 206, 115);
		
		// FIXME: This things shouldn't be calculated every frame
		String bestTimeText = game.getContext().getString(R.string.best_time) +
				": " + String.valueOf(bestTime == -1 ? 0 : bestTime);
		String mostCoinsText = game.getContext().getString(R.string.best_coins) +
				": " + String.valueOf(mostCoins);
		
		int x = (int) (canvas.getCanvas().getWidth() / 2  - textPaint.measureText(bestTimeText) / 2);
		int y = (int) (90 * DisplayConverter.getScreenRatioHeight());
		canvas.drawText(bestTimeText, x, y, textPaint);
		canvas.drawText(bestTimeText, x, y, strokePaint);
		
		x = (int) (canvas.getCanvas().getWidth() / 2  - textPaint.measureText(mostCoinsText) / 2);
		y = (int) (160 * DisplayConverter.getScreenRatioHeight());
		canvas.drawText(mostCoinsText, x, y, textPaint);
		canvas.drawText(mostCoinsText, x, y, strokePaint);
		
		for (Button b : buttons)
			b.render(canvas, timeElapsed);
	}

	@Override
	public void load() {
		anFactory.loadImages(PAUSED_BACKGROUND, "menu_button_selected", "menu_button_unselected",
				"about_selected", "about_unselected");
		createButtons();
	}

	@Override
	public void release() {
		anFactory.releaseImages(PAUSED_BACKGROUND, "menu_button_selected", "menu_button_unselected",
				"about_selected", "about_unselected");
		buttons.clear();
	}

	@Override
	public void input(MotionEvent event) {
		
		for (Button b : buttons)
			b.input(event);
		
	}

}
